package edu.pku.sei.transformation.editor.views.progreassmonitor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Display;

public class ThreadSafeProgressMonitor implements IProgressMonitor{

	private IProgressMonitor monitor;
	
	public ThreadSafeProgressMonitor(IProgressMonitor monitor) {
		this.monitor = monitor;
	}
	
	@Override
	public void beginTask(final String name, final int totalWork) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				monitor.beginTask(name, totalWork);

			}
		});
		
	}

	@Override
	public void done() {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				monitor.done();
			}
		});
		
	}

	@Override
	public void internalWorked(final double work) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				monitor.internalWorked(work);
			}
		});
		
	}

	@Override
	public boolean isCanceled() {
		return monitor.isCanceled();
	}

	@Override
	public void setCanceled(final boolean value) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				monitor.setCanceled(value);
			}
		});
		
	}

	@Override
	public void setTaskName(final String name) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				monitor.setTaskName(name);
			}
		});
		
	}

	@Override
	public void subTask(final String name) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				monitor.subTask(name);
			}
		});
		
	}

	@Override
	public void worked(final int work) {
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				monitor.worked(work);
			}
		});
		
	}
	
}
